package org.yray.sip.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.nustaq.serialization.FSTConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@Slf4j
public class SerializeUtils {
    private final  static FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();

    public static void serializeMap2File(Map hashMap,String fileName)
    {
        byte[] bytes = conf.asByteArray(hashMap);
        log.info("size={}",bytes.length);
        try {
            FileUtils.writeByteArrayToFile(new File(fileName),bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Map deserializeFile2Map(String fileName)
    {
        Map map =null;
        try {
            File file=new File(fileName);
            if(file.exists()) {
                byte[] bs = FileUtils.readFileToByteArray(file);
                map = (Map) conf.asObject(bs);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return map;
    }
}
